<template>
  <div class="custom_page formSearch">
    <div class="content">
      <!-- 顶部搜索 -->
      <div class="toolbar">
        <!-- 顶部搜索 -->
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="分销员昵称">
            <el-input
              v-model="searchForm.distributorName"
              maxlength="20"
              placeholder="请输入分销员昵称"
            />
          </el-form-item>
          <el-form-item label="分销员手机号">
            <el-input
              v-model="searchForm.distributorPhone"
              maxlength="11"
              placeholder="请输入手机号"
            />
          </el-form-item>
          <el-form-item label="申请时间">
            <el-date-picker
              v-model="searchForm.dates"
              type="daterange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="审核状态">
            <el-select v-model="searchForm.state" placeholder="请选择审核状态">
              <el-option label="全部" value="null" />
              <el-option label="待处理" :value="0" />
              <el-option label="审核通过" :value="1" />
              <el-option label="审核不通过" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="clear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!--  表格 -->
      <div class="content_table">
        <div class="table">
          <el-table
            v-loading="tableLoading"
            :data="tableData"
            border
            :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
            style="width: 100%"
            class="dataTable"
          >
            <el-table-column prop="distributorName" label="分销员昵称" />
            <el-table-column prop="distributorPhone" label="分销员手机号" />
            <el-table-column prop="inviteesName" label="邀请人" />
            <el-table-column prop="orderTotal" label="累计下单数" />
            <el-table-column prop="orderMoney" label="累计消费金额" />
            <el-table-column label="审核状态">
              <template #default="scope">
                <span v-if="scope.row.state == 0">待处理</span>
                <span v-if="scope.row.state == 1">审核通过</span>
                <span v-if="scope.row.state == 2">审核不通过</span>
              </template>
            </el-table-column>
            <el-table-column prop="money" label="审核时间" />
            <el-table-column label="操作" show-overflow-tooltip>
              <template #default="scope">
                <div class="btnList">
                  <el-button link type="primary" @click="audit(scope.row)">处理</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="fenye">
            <el-pagination
              v-model:current-page="searchForm.page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="searchForm.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              background
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </div>
    </div>

    <!-- 清退弹框 -->
    <el-dialog
      v-model="isVisible.show"
      :title="isVisible.title"
      center
      :close-on-click-modal="false"
      width="30%"
    >
      <div style="text-align: center">
        <div class="unbindimg">
          <img src="../../../assets/images/tixing.png" alt srcset />
        </div>
        <div class="dialog_content">请确认好分销员得信息再进行操作</div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="agreeEn(2)">审核不通过</el-button>
          <el-button type="primary" @click="agreeEn(1)">审核通过</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { getStayExamineAll, handle } from '@/api/distributor'

const searchForm = ref({
  distributorName: '',
  distributorPhone: '',
  dates: [],
  state: 0,
  page: 1,
  pageSize: 10
})
const isVisible = ref({})
const total = ref(1)
const tableLoading = ref(false)
const tableData = ref([])

onBeforeMount(() => {
  getAll()
})
const handleSizeChange = (val) => {
  searchForm.value.pageSize = val
  getAll()
}
const handleCurrentChange = (val) => {
  searchForm.value.page = val
  getAll()
}
// 查询
const search = () => {
  total.value = 1
  searchForm.value.page = 1
  getAll()
}
const agreeEn = async (index) => {
  if (index === 1) {
    const res = await handle({
      distributorId: isVisible.value.distributorId,
      state: 1,
    })
    if (res.code === '') {
      ElMessage.success('成功')
      getAll()
      isVisible.value.show = false
    }
  } else if (index === 2) {
    const res = await handle({
      distributorId: isVisible.value.distributorId,
      state: 2,
    })
    if (res.code === '') {
      ElMessage.success('成功')
      getAll()
      isVisible.value.show = false
    }
  }
}
// 清退
const audit = (row) => {
  isVisible.value = {
    show: true,
    title: '审核',
    index: 3,
    distributorId: row.distributorId,
  }
}
// 清除
const clear = () => {
  searchForm.value = {
    distributorName: '',
    distributorPhone: '',
    dates: [],
    state: 0,
    page: 1,
    pageSize: 10,
  }
  getAll()
}
// 初始化查询所有数据
const getAll = async () => {
  try {
    tableLoading.value = true
    const res = await getStayExamineAll(searchForm.value)
    tableData.value = res.data.list
    total.value = res.data.total
  } finally {
    tableLoading.value = false
  }
}
</script>

<style lang="scss" scoped>
// .custom_page {
//   padding: 20px 20px 0;
// }
.checkBoxStyle {
  margin-bottom: 20px;
}
.dialog_content {
  width: 275px;
  height: 46px;
  margin: auto;
  font-size: 16px;
  font-family: PingFang SC;
  font-weight: 400;
  color: #333333;
  line-height: 30px;
  margin-top: 25px;
}
</style>
